package UILayer;
import java.util.Scanner;
import ControlLayer.ItemCtr;
import ModelLayer.Item;

/**
 * Write a description of class ItemUI here.
 * 
 * @author (Gruppe 2) 
 * @version (a version number or a date)
 */
public class ItemUI
{
    private ItemCtr itemCtr;

    public ItemUI()
    {
        itemCtr = new ItemCtr();
        start(itemCtr);
    }

    public void start (ItemCtr ctr)
    {
        itemCtr = ctr;
        itemMenu();
    }

    public void itemMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeItemMenu();
            if (choice == 1)
            {
                System.out.println("\f");
                findItem(inputItemId());
            }
            if (choice == 2)
            {
                System.out.println("\f");
                createItem(inputItemId(), inputItemDescription(), inputItemStatus(), inputItemStorage(), inputItemPrice());
            }
            if (choice == 3)
            {
                System.out.println("\f");
                deleteItem(inputItemId());
            }
            if (choice == 4)
            {
                System.out.println("\f");
                printAll();
            }
            if (choice == 5)
            {
                exit = true;
                System.out.println("\f");
            }
        }
    }

    private int writeItemMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(""); //This blank line are made so that everything wont be printed right on top of each other
        System.out.println("*** Item Menu ***");
        System.out.println(" (1) Show Item information");
        System.out.println(" (2) Create Item");
        System.out.println(" (3) Delete Item");
        System.out.println(" (4) Show all items");
        System.out.println(" (5) Return");
        System.out.print("\n Make your choice: ");

        boolean done = false;
        int choice = 0;
        while(!done)
        {
            try
            {
                choice = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return choice;
    }

    private int inputItemId()
    {
        boolean done = false;
        int id = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type Item ID: ");
            try
            {
                id = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }

    private String inputItemDescription()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Item description: ");
        String description = keyboard.nextLine();
        return description;
    }

    private String inputItemStatus()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Item status: ");
        String status = keyboard.nextLine();
        return status;
    }

    private String inputItemStorage()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Item storage: ");
        String storage = keyboard.nextLine();
        return storage;
    }

    private double inputItemPrice()
    {
        boolean done = false;
        double price = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Type Item price: ");
            try
            {
                price = keyboard.nextDouble();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return price;
    }

    private void findItem(int id)
    {
        itemCtr.printItem(id);
    }

    private void createItem(int id, String description, String status, String storage, double price)
    {
        itemCtr.createItem(id, description, status, storage, price);
    }

    private void deleteItem(int id)
    {
        itemCtr.removeItem(id);
    }
    
    private void printAll()
    {
        itemCtr.printItemList();
    }
    

}
